package com.servicesystem.servicesystem.controller.ali;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.servicesystem.servicesystem.entity.AliOrderLogistics;
import com.servicesystem.servicesystem.service.AliOrderLogisticsService;
import com.servicesystem.servicesystem.service.OrderService;
import com.servicesystem.servicesystem.service.ali.AliOrderService;
import com.servicesystem.servicesystem.untis.R;
import com.servicesystem.servicesystem.untis.ali.MessageType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("message")
public class messageController {

    @Autowired
    private AliOrderLogisticsService aliOrderLogisticsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AliOrderService aliOrderService;
    //private final static HttpRequestAll httpRequestAll = new HttpRequestAll();

    /**
     * 1688订单发货（买家视角）
     * 1688产品删除（关系用户视角）
     * 1688产品下架（关系用户视角）
     */
    @PostMapping("message-receiver")
    public R orderSend(String message, String _aop_signature) {
        Map map = gsonParse(message);
        Object type = map.get("type");
        switch (type.toString()) {
            case MessageType.ORDER_SEND:
                // 订单发货
//                String orderId = ((Map) map.get("data")).get("orderId").toString();
//                R logisticsInfo = aliOrderService.getLogisticsInfo(orderId);
//                String message1 = logisticsInfo.getMessage();
//                Map map1 = gsonParse(message1);
//                List list = (List) map1.get("result");
//                if ((boolean) map1.get("success")) {
//                    list.forEach(item -> {
//                        Map i = (Map) item;
//                        updateOrderLogistics(orderId, i.get("logisticsBillNo").toString(), i.get("logisticsCompanyName").toString());
//                    });
//                    /** 字节需要 标记*/
//                    AliOrderLogistics logistics = aliOrderLogisticsService.getLogisticsByAliOrderId(orderId);
//                    if (logistics != null) {
//                        String ttOrderId = logistics.getTtOrderId();
//                        orderService.upDataOrderOrderStatus(ttOrderId);
//                    }
//                }
                break;
            case MessageType.PRODUCT_DELETE:
                // 产品删除
                String products = (String) ((Map) map.get("data")).get("productIds");
                String[] split = products.split(",");
                Arrays.asList(split).forEach(System.out::println);
                break;
            case MessageType.PRODUCT_SHELF:
                // 产品下架
                products = (String) ((Map) map.get("data")).get("productIds");
                System.out.println("产品下架" + products);
                split = products.split(",");
                Arrays.asList(split).forEach(System.out::println);
                break;
            case MessageType.PRODUCT_RECEIVE:
                Map reData = ((Map<Object, Object>) ((Map<Object, Object>) map.get("data")).get("OrderLogisticsTracingModel"));
                if ("ACCEPT".equals(reData.get("statusChanged").toString())) {
                    List orderIds = ((List) reData.get("orderLogsItems"));
                    for (Object orderIdMap : orderIds) {
                        String orderId = ((Map<Object, Object>) orderIdMap).get("orderId").toString();
                        R logisticsInfo = aliOrderService.getLogisticsInfo(orderId);
                        String message1 = logisticsInfo.getMessage();
                        Map map1 = gsonParse(message1);
                        List list = (List) map1.get("result");
                        if ((boolean) map1.get("success")) {
                            list.forEach(item -> {
                                Map i = (Map) item;
                                updateOrderLogistics(orderId, i.get("logisticsBillNo").toString(), i.get("logisticsCompanyName").toString());
                            });
                            /** 字节需要 标记*/
                            AliOrderLogistics logistics = aliOrderLogisticsService.getLogisticsByAliOrderId(orderId);
                            if (logistics != null) {
                                String ttOrderId = logistics.getTtOrderId();
                                orderService.upDataOrderOrderStatus(ttOrderId);
                            }
                        }
                    }
                }
                break;
        }
        return R.success();
    }

    @GetMapping("message-total")
    public R getMessageTotal() {

        return R.success();
    }

    private R updateOrderLogistics(String aliOrderId, String logisticsId, String logisticsName) {
        aliOrderLogisticsService.fixLogistics(aliOrderId, logisticsId, logisticsName);
        return R.success();
    }

    private Map gsonParse(String message) {
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(message);
        return JSON.parseObject(jsonElement.toString(), Map.class);
    }
}
